package easy.id_0_100;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Comparator;

public class hua11 {

    public static void main(String[] args) {
        float demand=20;
        float[] inventory={18, 15, 10};
        float[] price={75, 72, 45};
        System.out.println(phoneCaseInventoryManage(demand, inventory, price));
    }

    // 待实现函数，在此函数中填入答题代码
    private static String phoneCaseInventoryManage(float demand, float[] inventory, float[] price) {
        float[] singlePrice=new float[inventory.length];
        Integer[] indexes = new Integer[inventory.length];
        for (int i = 0; i < inventory.length; i++) {
            indexes[i] = i;
        }
        for(int i=0;i<inventory.length;i++){
            singlePrice[i]=price[i]/inventory[i];
        }
        Comparator<Integer> comparator = new Comparator<Integer>(){
            @Override
            public int compare(Integer a, Integer b) {
                if(singlePrice[a]-singlePrice[b]>=0){
                    return -1;
                }
                return 1;
            }
        };
        Arrays.sort(indexes,comparator);
        float curdemand=0;
        float curmoney=0;
        for(int i=0;i< indexes.length;i++){
            int index=indexes[i];
            if(curdemand+inventory[index]<=demand){
                curmoney=curmoney+price[index];
                curdemand=curdemand+inventory[index];
            }else{
                curmoney=curmoney+(price[index]/inventory[index])*(demand-curdemand);
                break;
            }
        }
        DecimalFormat decimalFormat = new DecimalFormat("#.00");

        return decimalFormat.format(curmoney);
    }

}
